package ca.ubc.cs304.library.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.WindowConstants;

import ca.ubc.cs304.library.sql.OracleDatabaseWrapper;
import ca.ubc.cs304.library.Configuration;

public class LibLogin {

	private static final int CPNT_INDENT = 20;	
	private static final int CPNT_HEIGHT = 30;
	private static final int CPNT_WIDTH = 220;

	private static final int LABL_X = 20;
	private static final int TEXT_X = 140;

	
	private static final String LABEL [] = {"University Library Application v1.0", "Connect URL", "Username", "Password"};	
	private static final String BUTTON [] = {"Connect", "Cancel"};	
	
	final private JFrame frame 			= new JFrame("University Library Application v1.0 Login");
	
	final private JLabel [] label 		= new JLabel [4];
	final private JButton [] button 	= new JButton [2];
	final private JTextField [] text 	= new JTextField [2];

	final private JPasswordField passwordText	= new JPasswordField();
	private OracleDatabaseWrapper sql;	
	private JFrame mainFrame;
	private JMenu [] menu;

	public LibLogin(OracleDatabaseWrapper sql, JFrame frame, JMenu [] menu) {
		this.sql = sql;
		this.mainFrame = frame;
		this.menu = menu;
		initialize();
		
		//frame.toFront();
	}

	private void initialize() {
		
		frame.setBounds(450, 250, 400,300);
		frame.setResizable(false);
		//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		initLogin();
	
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

		frame.setVisible(true);	
	}
	
	private void initLogin() {
		int y = 20;
		for(int i = 0; i < text.length || i < label.length ; i++) {
			label[i] = new JLabel(LABEL[i]);  
			label[i].setBounds(LABL_X, y, CPNT_WIDTH, CPNT_HEIGHT);
			
			y += CPNT_HEIGHT + CPNT_INDENT;
			
			frame.getContentPane().add(label[i]);
		}
		
		int x = 60;
		y = 65;
		for(int i = 0; i < button.length ; i++) {
			button[i] = new JButton (BUTTON[i]);
			text[i] = new JTextField();
			
			button[i].setBounds(x, 215, 140, CPNT_HEIGHT);
			text[i].setBounds(TEXT_X, y, CPNT_WIDTH, CPNT_HEIGHT);
			text[i].setColumns(10);
			
			x += 140 + CPNT_INDENT;
			y += CPNT_HEIGHT + CPNT_INDENT;
	
			frame.getContentPane().add(button[i]);	
			frame.getContentPane().add(text[i]);			
					
		}
		passwordText.setBounds(TEXT_X, y, CPNT_WIDTH, CPNT_HEIGHT);
		frame.getContentPane().add(passwordText);

		text[0].setText(Configuration.oracleLocation);		
		text[1].setText(Configuration.oracleUsername);
		passwordText.setText(Configuration.oraclePassword);
		
		// Ok Button
		button[0].addActionListener(new ActionListener () {
			public void actionPerformed(ActionEvent arg0) {
				String [] param = new String [3];
				
				for(int i = 0 ; i < text.length ; i++) {
					
					if(text[i].getText().equals("") || passwordText.getText().equals("")) {
						JOptionPane.showMessageDialog(frame,
							    "Missing input field(s)",
							    "Warning",
							    JOptionPane.WARNING_MESSAGE);	
						return;
					} else {
						param[i] = text[i].getText();
					}
				}
				
				try {
					if(sql.connect(param[1], passwordText.getText(), param[0])) {
						mainFrame.setTitle("University Library Application v1.0: " + param[1] + " " + param[0]);
						menu[0].setEnabled(false);
						menu[1].setEnabled(true);						
						frame.setVisible(false);
					} 
				} catch (SQLException e) {
					JOptionPane.showMessageDialog(frame,
							e.getMessage(),
						    "Error",
						    JOptionPane.ERROR_MESSAGE);						
				} catch (NullPointerException e) {
					JOptionPane.showMessageDialog(frame,
							e.getMessage(),
						    "Error",
						    JOptionPane.ERROR_MESSAGE);						
				}
			}					
		});
		
		// Cancel Button
		button[1].addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frame.setVisible(false);
			}
		});	
	}
}
